<?php
// Report simple running errors
error_reporting(E_ERROR | E_WARNING | E_PARSE);
require('D:/Projects/gagcrawler/includes/functions.php');
require('D:/Projects/gagcrawler/includes/simple_html_dom.php');
$latestUrl = 'www.ovui.vn/new/';
function getOvuiPost($latestUrl){
	$ch = curl_init();

	$header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,";
	$header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
	$header[] = "Cache-Control: max-age=0";
	$header[] = "Connection: keep-alive";
	$header[] = "Keep-Alive: 300";
	$header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
	$header[] = "Accept-Language: en-us,en;q=0.5";
	$header[] = "Pragma: "; // browsers keep this blank.
	// set URL and other appropriate options
	curl_setopt($ch, CURLOPT_URL, $latestUrl);
	curl_setopt($ch, CURLOPT_HEADER, 0);
	curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
	//curl_setopt($ch, CURLOPT_PROXY, '210.245.0.24:80');
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

	// grab URL and pass it to the browser
	$responseContent = curl_exec($ch);
	curl_close($ch);
	$html = str_get_html($responseContent);
	$type = 'photo';
	$postTitle = '';
	$postImage = '';
	$videoCode = '';
	$postLink = '';
	$postType = 'photo';

	if(trim($html) != ''){
		foreach($html->find('#entries-content-ul li') as $element){
			$h1Object = $element->find('h1', 0);
			if($h1Object){
				$aTitle = $h1Object->find('a', 0);
				$postTitle = strip_tags($aTitle->innertext);
				$postContain = $element->find('div.content', 0);
				$linkObj = $postContain->find('a', 0);
				$postLink = $linkObj->href;
				$postImage = $linkObj->find('img', 0)->src;
				if(strstr($postImage, 'loading.gif')){
					$postImage = preg_replace('/.+data-original="(.*?)".+/is', '$1', $linkObj->innertext);
				}
				if(strstr($postImage, 'i.ytimg.com')){
					$postType = 'video';
					$videoCode = preg_replace('/.+vi\/(.*?)\/.+/is', '$1', $postImage);
				}
				else{
					$postType = 'photo';
					$videoCode = '';
				}
				if(trim($postTitle) == '' || trim($postImage) == '' || trim($postLink) == '') continue;
				$unique = md5($postImage);
				echo "\tcrawling [".end(explode('/',$postLink))."] .. ";
				$postTitle = mysql_real_escape_string(strip_tags($postTitle));
				$postLink = mysql_real_escape_string($postLink);
				if(strstr($postImage, ':8080')){
					$fName = end(explode('/', $postImage));
					$postImage = str_replace($fName, 'l-'.$fName, $postImage);
				}
				$fileInfo = imageDownloader($postImage);
				if(!$fileInfo) continue;
				$postImage = $fileInfo['name'];
				if($postType == 'photo'){
					if(isAnimatedImage(IMAGE_PATH.$fileInfo['name'])){
						$postType == 'gif';
					}
				}
				$parse = parse_url($postLink);
				$sqlInsert = "INSERT INTO data(original_url, post_title, post_image, post_unique, crawl_date, file_checksum,item_type,video_code,source_page) VALUES('".$postLink."', '$postTitle', '$postImage', '$unique', NOW(), '".$fileInfo['checksum']."', '$postType', '$videoCode', '".$parse['host']."')";
				if(mysql_query($sqlInsert)){
					echo "\tdone\n";
				}
				else{
					echo "\tfailed -> \n".mysql_error()."\n\n";
					#die("-------BREAK-------");
				}
			}
		}
	}
}
require('D:/Projects/gagcrawler/dbconnect.php');
#echo '<ol>';
$numPage = 20;
for($i = 1; $i <=$numPage; $i++){
	#echo '<li>';
	echo "----------------------\nCrawling page $i/$numPage:\n";
	getOvuiPost($latestUrl.$i);
	sleep(5);
	#echo '</li>';
}
echo "\n----------------------[DONE]----------------------\n";